﻿using Ace.Application;
using Ace.Data;
using Ace.Entity.Wiki;
using Ace.Exceptions;
using Ace.IdStrategy;
using Chloe;
using Chloe.MySql;
using Chloe.SqlServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ace.Application.Wiki
{
    public interface IVideoLogService : IAppService
    {
        List<VideoLog> GetList(string Fid = "", string UserID = "");

       

        void Add(AddVideoLogInput input);
        void Update(UpdateVideoLogInput input);
        void Delete(string id );

        VideoLog GetModel(string Id);

        PagedData<VideoLog> GetPageData(Pagination page, string Fid = "", string UserID = "");

        PagedData<VideoLogInfo> GetVideoLogPageData(Pagination page, string Fid = "", string UserID = "", int ST1 = -1, int ST2 = -1, int ST3 = -1);


        int Count(string Id, string UserID);
        VideoLogChart GetCount(string Fid, string UserID = "");


        void UpdateST(string Fid, string UserID, int ST1 = -1, int ST2 = -1, int ST3 = -1);
    }

    public class VideoLogService : AppServiceBase<VideoLog>, IVideoLogService
    {
        public VideoLogService(IDbContext dbContext, IServiceProvider services) : base(dbContext, services)
        {
        }

        public List<VideoLog> GetList(string Fid = "", string UserID = "")
        {
            var q = this.Query;

          

            if (Fid.IsNotNullOrEmpty())
            {
                q = q.Where(a => a.Fid== Fid);
            }
            if (UserID.IsNotNullOrEmpty())
            {
                q = q.Where(a => a.UserID == UserID);
            }

            var ret = q.ToList();
            return ret;
        }
        

        public void Add(AddVideoLogInput input)
        {
            this.InsertFromDto(input);
        }
        public void Update(UpdateVideoLogInput input)
        {
            this.UpdateFromDto(input);
        }

        public VideoLog GetModel(string Id)
        {
            return  this.Query.Where(a => a.Id == Id).FirstOrDefault();
        }

        public void Delete(string id)
        {
            this.DbContext.Delete<VideoLog>(a => a.Id == id);
        }


        public PagedData<VideoLog> GetPageData(Pagination page, string Fid = "", string UserID = "")
        {
            var q = this.DbContext.Query<VideoLog>();

            if (Fid.IsNotNullOrEmpty())
            {
                q = q.Where(a => a.Fid == Fid);
            }
            if (UserID.IsNotNullOrEmpty())
            {
                q = q.Where(a => a.UserID == UserID);
            }
             


            PagedData<VideoLog> pagedData = q.TakePageData(page);
            
            return pagedData;
        }

         
        public int Count(string Id, string UserID)
        {
            int count= this.DbContext.Query<VideoLog>().Where(a => a.Fid == Id && a.UserID == UserID).Count();
            return count;
        }

        public VideoLogChart GetCount(string Fid,string UserID="")
        {
            int Count1 = this.DbContext.Query<VideoLog>().Where(a=>a.Fid==Fid&&a.ST1==1).Count();
            int Count2 = this.DbContext.Query<VideoLog>().Where(a => a.Fid == Fid && a.ST2 == 1).Count();
            int Count3 = this.DbContext.Query<VideoLog>().Where(a => a.Fid == Fid && a.ST3 == 1).Count();

            int PlayNum= this.DbContext.Query<VideoLog>().Where(a => a.Fid == Fid).Count();

            int Today= this.DbContext.Query<VideoLog>().Where(a => a.Fid == Fid && a.CreateDate>=DateTime.Now.AddDays(-1)).Count();

            string sql = "SELECT b.UserIcon FROM video_log a LEFT JOIN users b ON a.UserID=b.Id where a.Fid='"+Fid+"' limit 4;";


            int ST1 = 0;
            int ST2 = 0;
            int ST3 = 0;

            if(!string.IsNullOrEmpty(UserID))
            {
                VideoLog modle= this.Query.Where(a => a.Fid == Fid && a.UserID== UserID).FirstOrDefault();
                ST1 = modle.ST1;
                ST2 = modle.ST2;
                ST3 = modle.ST3;
            }

            VideoLogChart chart = new VideoLogChart();
            chart.Count1 = Count1;
            chart.Count2 = Count2;
            chart.Count3 = Count3;
            chart.PlayNum = PlayNum;
            chart.Today = Today;

            chart.ST1 = ST1;
            chart.ST2 = ST2;
            chart.ST3 = ST3;

            chart.UserList = this.DbContext.SqlQuery<VideoLogUser>(sql).ToList();
            return chart;
        }

        public void UpdateST(string Fid,string UserID,int ST1=-1, int ST2=-1, int ST3=-1)
        {
            if(ST1>-1)
            {
                this.DbContext.Update<VideoLog>(a => a.Fid == Fid && a.UserID==UserID , a => new VideoLog()
                {
                    ST1 = ST1
                });
            }

            if (ST2 > -1)
            {
                this.DbContext.Update<VideoLog>(a => a.Fid == Fid && a.UserID == UserID, a => new VideoLog()
                {
                    ST2 = ST2
                });
            }

            if (ST3 > -1)
            {
                this.DbContext.Update<VideoLog>(a => a.Fid == Fid && a.UserID == UserID, a => new VideoLog()
                {
                    ST3 = ST3
                });
            }


        }



        public PagedData<VideoLogInfo> GetVideoLogPageData(Pagination page, string Fid = "", string UserID = "", int ST1 = -1, int ST2 = -1, int ST3 = -1)
        {
            string strFileds = " a.*,b.Title,b.ImageUrl ";

            string strWhere = " 1=1 ";



            if (!string.IsNullOrEmpty(Fid))
            {
                strWhere += " and a.Fid='"+ Fid + "'";
            }

            if (!string.IsNullOrEmpty(UserID))
            {
                strWhere += " and a.UserID='" + UserID + "'";
            }

            if (ST1 > -1)
            {
                strWhere += " and a.ST1=" + ST1;
            }
            if (ST2 > -1)
            {
                strWhere += " and a.ST2=" + ST2;
            }
            if (ST3 > -1)
            {
                strWhere += " and a.ST3=" + ST3;
            }




            DbParam _totalcount = new DbParam("?_totalcount", null, typeof(int)) { Direction = ParamDirection.Output };
            DbParam _pagecount = new DbParam("?_pagecount", null, typeof(int)) { Direction = ParamDirection.Output };

            DbParam[] dbs = new DbParam[] {
                new DbParam("?_fields", strFileds),
                new DbParam("?_tables", " video_log a left join video b on a.Fid=b.Id "),
                new DbParam("?_where",strWhere),
                new DbParam("?_orderby", "a.CreateDate desc"),
                new DbParam("?_pageindex", page.Page),
                new DbParam("?_pageSize", page.PageSize),
                _totalcount,
                _pagecount
        };

            List<VideoLogInfo> list = this.DbContext.SqlQuery<VideoLogInfo>("sp_viewPage", System.Data.CommandType.StoredProcedure, dbs).ToList();


            PagedData<VideoLogInfo> pagedData = new PagedData<VideoLogInfo>();
            pagedData.CurrentPage = page.Page;
            pagedData.Models = list;
            pagedData.PageSize = page.PageSize;
            pagedData.TotalCount = Convert.ToInt32(_totalcount.Value);
            pagedData.TotalPage = Convert.ToInt32(_pagecount.Value);

            return pagedData;
        }



    }

}
